/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.yarolegovich.lovelydialog;

import ohos.agp.animation.Animator;
import ohos.agp.components.AttrSet;
import ohos.agp.components.Component;
import ohos.agp.render.Arc;
import ohos.agp.render.Canvas;
import ohos.agp.render.Paint;
import ohos.agp.utils.Color;
import ohos.agp.utils.RectFloat;
import ohos.app.Context;

/**
 * 圆形自定义component
 */
public class CircleRingComponent extends BaseComponent implements Component.DrawTask {
    private Paint mPaint;
    private Paint mPaintPro;
    private float mWidth = 0f;
    private float mPadding;
    private float startAngle = 0f;
    private float sweepAngle = 50f;
    private float value;
    private RectFloat rectF = new RectFloat();


    public CircleRingComponent(Context context) {
        this(context, null);
    }

    public CircleRingComponent(Context context, AttrSet attrSet) {
        this(context, attrSet, null);
    }

    public CircleRingComponent(Context context, AttrSet attrSet, String styleName) {
        super(context, attrSet, styleName);
        mPadding = 15;
        startAnim();
    }

    /**
     * 初始化画笔
     */
    @Override
    protected void initPaint() {
        mPaint = new Paint();
        mPaint.setAntiAlias(true);
        mPaint.setStyle(Paint.Style.STROKE_STYLE);
        mPaint.setColor(Color.BLUE);
        mPaint.setStrokeWidth(18);
        mPaintPro = new Paint();
        mPaintPro.setAntiAlias(true);
        mPaintPro.setStyle(Paint.Style.STROKE_STYLE);
        mPaintPro.setColor(Color.LTGRAY);
        mPaintPro.setStrokeWidth(18);
        addDrawTask(this);
    }

    /**
     * 设置画笔颜色
     *
     * @param color color
     */
    public void setViewColor(int color) {
        mPaintPro.setColor(new Color(color));
        invalidate();
    }

    /**
     * 设置画笔颜色
     *
     * @param color color
     */
    public void setBarColor(int color) {
        mPaint.setColor(new Color(color));
        invalidate();
    }

    /**
     * 动画刷新
     *
     * @param value value
     */
    @Override
    protected void onAnimationUpdate(float value) {
        this.value = value;
        startAngle = 360 * value;
        invalidate();
    }

    /**
     * 重复动画
     *
     * @param animation animation
     */
    @Override
    protected void onAnimationRepeat(Animator animation) {
    }

    /**
     * 动画停止
     *
     * @return int
     */
    @Override
    protected int onStopAnim() {
        return 0;
    }

    /**
     * 重复模式
     *
     * @return int
     */
    @Override
    protected int setAnimRepeatMode() {
        return RESTART;
    }

    /**
     * 动画重复次数
     *
     * @return int
     */
    @Override
    protected int setAnimRepeatCount() {
        return Integer.MAX_VALUE;
    }

    /**
     * 动画执行中
     */
    @Override
    protected void ainmIsRunning() {
    }

    /**
     * 动画绘制
     *
     * @param component component
     * @param canvas    canvas
     */
    @Override
    public void onDraw(Component component, Canvas canvas) {
        if (getWidth() > getHeight()) {
            mWidth = getHeight();
        } else {
            mWidth = getWidth();
        }
        if (value <= 0.5f) {
            sweepAngle = 50.0f + 300.f * value;
        } else {
            sweepAngle = 200.0f - 300.f * (value - 0.5f);
        }
        rectF = new RectFloat(mPadding, mPadding, mWidth - mPadding, mWidth - mPadding);
        canvas.drawArc(rectF, new Arc(startAngle, sweepAngle, false), mPaint);
    }
}
